import { onLoad, onShow } from '@dcloudio/uni-app';
import { ref, watch } from 'vue';
import backBtn from '../../common/backBtn/backBtn.vue'
import { objectAny } from '../../options/base';
import { mySchoolStore } from '../../store';
import { showModal, showToast } from '../../utils/utils';



async function getSchoolList() {
	const db = uniCloud.database();
	const dbCmd = db.command
	const $ = dbCmd.aggregate
	const { result } = await db.collection(`admin-school-list`)
		.field(`enable_status,school_name,school_icon,city_id_array,city_code`)
		.get()
		// .aggregate()
		// .lookup({
		// 	from: `admin-city-china`,
		// 	as: 'cityChina',
		// 	let: {
		// 		city_code: `$city_code`
		// 	},
		// 	// @ts-ignore
		// 	pipeline: $.pipeline()
		// 		// @ts-ignore
		// 		.match(dbCmd.expr(
		// 			$.eq(['$code', '$$city_code'])
		// 		))
		// 		.done()
		// }).end();

	return result
}

function getMyLocationToActual() {

}

export default {
	components: {
		backBtn
	},
	setup() {
		const queryValue = ref('');
		const activeIndex = ref(0);
		const scrollViewLeft = ref(0);
		const scrollViewWidth = ref(0);
		const querySchoolList = ref([] as Array<objectAny>);
		const childSchoolList=ref([] as Array<objectAny>)
		const schoolList = ref([] as Array<objectAny>)

		let deviceWidth = 0;

		onLoad(async (options) => {
			const { windowHeight, windowWidth } = uni.getSystemInfoSync();
			deviceWidth = windowWidth || 0;

			const { data } = await getSchoolList();

			const _cache1: objectAny = {
				[activeIndex.value]: {
					list: [],
					name: `所有的`,
					_id: activeIndex.value
				}
			}

			for (let i = 0; i < data.length; i++) {
				const it = data[i];
				const {_id}=it
				const { value, text } = it.city_id_array[0];

				_cache1[activeIndex.value].list.push(it);
				if (_cache1[value] == null) {
					_cache1[value] = {
						list: [it],
						name:text,
						_id
					};
				} else {
					_cache1[value].list.push(it)
				}

			}
			childSchoolList.value=Object.values(_cache1)
			scrollViewWidth.value = uni.upx2px(140) * Object.keys(_cache1).length
			toggleTab(activeIndex.value,_cache1[activeIndex.value+'']);

		})

		watch(activeIndex, (value) => {
			if (value >= 3) {
				scrollViewLeft.value = deviceWidth / 2
			} else {
				scrollViewLeft.value = 0
			}
		})

		const toggleTab = (index:number,it:objectAny) => {
			activeIndex.value = index;
			const list = childSchoolList.value[index].list.sort((a: objectAny, b: objectAny) => (+b.enable_status) - (+a.enable_status))
			schoolList.value = list
		}

		const queryMySchool = () => {
			const name = queryValue.value.trim();
			if (name === '') {
				querySchoolList.value = [];
			} else {
				querySchoolList.value = schoolList.value.filter(it => it.school_name.includes(name));
			}
		}

		const toogleSchool = (it: objectAny) => {
			if (!it.enable_status) {
				return showModal({
					title: `提示`,
					showCancel: false,
					content: `暂未开放请期待`
				});
			}

			mySchoolStore().setMySchool(it)
			
			uni.navigateBack({
				delta: 1,
				success: () => {
					showToast(`切换成功`, `success`)
				}
			})
		}

		return {
			schoolList,
			scrollViewLeft,
			scrollViewWidth,
			activeIndex,
			querySchoolList,
			queryValue,
			childSchoolList,
			toggleTab,
			toogleSchool,
			queryMySchool
		}
	}
}